package com.ruoyi.purchase.controller;

import com.ruoyi.purchase.domain.FirmClient;
import com.ruoyi.purchase.service.IFirmClientService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 客户Controller
 * 
 * @author ruoyi
 * @date 2023-07-16
 */
@RestController
@RequestMapping("/client")
public class FirmClientController extends BaseController
{
    @Autowired
    private IFirmClientService firmClientService;

    /**
     * 查询客户列表
     */
    @RequiresPermissions("purchase:client:list")
    @GetMapping("/list")
    public TableDataInfo list(FirmClient firmClient)
    {
        startPage();
        List<FirmClient> list = firmClientService.selectFirmClientList(firmClient);
        return getDataTable(list);
    }

    /**
     * 导出客户列表
     */
    @RequiresPermissions("purchase:client:export")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FirmClient firmClient)
    {
        List<FirmClient> list = firmClientService.selectFirmClientList(firmClient);
        ExcelUtil<FirmClient> util = new ExcelUtil<FirmClient>(FirmClient.class);
        util.exportExcel(response, list, "客户数据");
    }

    /**
     * 获取客户详细信息
     */
    @RequiresPermissions("purchase:client:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(firmClientService.selectFirmClientById(id));
    }

    /**
     * 新增客户
     */
    @RequiresPermissions("purchase:client:add")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FirmClient firmClient)
    {
        return toAjax(firmClientService.insertFirmClient(firmClient));
    }

    /**
     * 修改客户
     */
    @RequiresPermissions("purchase:client:edit")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FirmClient firmClient)
    {
        return toAjax(firmClientService.updateFirmClient(firmClient));
    }

    /**
     * 删除客户
     */
    @RequiresPermissions("purchase:client:remove")
    @Log(title = "客户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(firmClientService.deleteFirmClientByIds(ids));
    }
}
